package com.shenbianyun.cloudcaring;

import com.alibaba.fastjson.JSONObject;
import com.shenbianyun.bosskgpay.constant.MerInfoConstant;
import com.shenbianyun.bosskgpay.entity.RequestMessage;
import com.shenbianyun.bosskgpay.entity.cloudcaring.EmployeeRecycleQuotaModel;
import com.shenbianyun.bosskgpay.entity.cloudcaring.SaveEmployeeReqModel;
import com.shenbianyun.bosskgpay.utils.*;
import org.apache.jmeter.config.Arguments;
import org.apache.jmeter.protocol.java.sampler.AbstractJavaSamplerClient;
import org.apache.jmeter.protocol.java.sampler.JavaSamplerContext;
import org.apache.jmeter.samplers.SampleResult;

/**
 * @author ：张一凡
 * @date ：2025年02月26日 11:33:12
 * @description： 14.1 麦粒回收
 */

public class EmployeeRecycleQuotaTest extends AbstractJavaSamplerClient {

    public final String CHARSET_UTF_8 = "UTF-8";
    public final Long MER_ID = 1711459098975825L;
    public static String MER_PRIVATE_KEY = "MIICdgIBADANBgkqhkiG9w0BAQEFAASCAmAwggJcAgEAAoGBALsaWzg/FFuHnS2z0TlNmFo1M0KJ+rlrZUsiaZPBgd3qaT1uVRqISIYGWrV0EYP5tRLNz4G9u1NuFW60pdxN/n5utTkD5bNEGSXC/zauUMctVN8Ccdmm+HI9DXDkJ+oyEjOA8nSuNe+T+pzzYzdQYcU1rj95dmgRcVRFV4Ao8+JHAgMBAAECgYBfl2ChhQDWnpu8PNGZMD61X9bXmP0i/G6dC3DbT5MVGO05uIP8cUJ0TwzLBgjo34wWllX9GD17q5OBowPWYXayrCU4CsqVBwEejJb8WQURZMB4IUbqeJNRhcuMZguNCxpTAujpdLdRnGT94d+ZHMJt194o3FBMTikS38hAvzrvGQJBANsS/7oE0o+LdLkrp5iiEkKw4QNqDb0u9vot54oY5dak5pvS/GLoXDXUSGAMyKu7YH0/5NMKJKaP1TflbCA17hUCQQDao84UtpsxqobcBGCDzrk+0gr+7E38OuGe3306luchYNJfEHdfMuPHjGABwLMRthnGeeS8JJ5h1zxMnPlcw0HrAkB0VM1pCUCKqyIrZiwUGBQcR/Lkrj9nc3YA+blXEsuRxHUUz9/n4b19dCGOp5sz3kdpmzE2g89ch0pQ6UoWfKzVAkEAjHzay6Z2XFiFpVROrAMmNv6CQEGY7qzrEsnqTr5uL51mL92Fxq8Ne8hwXE/1ADlxoH3sgxRIjPffcB8hgACbFwJAfzrLuOd81YrlmxmLZ8Y1Q+ykMTn4zl94C/spU05Cmc9gI0/FLDqUR7yzTXv1kV32f3jg2rUuWC1A3uLhRnM+0g==";
    public static String MER_PUBLIC_KEY = "MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQC7Gls4PxRbh50ts9E5TZhaNTNCifq5a2VLImmTwYHd6mk9blUaiEiGBlq1dBGD+bUSzc+BvbtTbhVutKXcTf5+brU5A+WzRBklwv82rlDHLVTfAnHZpvhyPQ1w5CfqMhIzgPJ0rjXvk/qc82M3UGHFNa4/eXZoEXFURVeAKPPiRwIDAQAB";
    public final String INTER_KEY = "44E71A1B0185923ABB52C40473767079";
    public final String API_VERSION = "V1.0";
    public final String OMS_TEST_URL = "http://api.merchant.serviceshare.com/clientapi/clientBusiness/common";

    private static String employeeNo = "88888888";//1913、88888888






    //设置可用参数及的默认值；
    @Override
    public Arguments getDefaultParameters() {
        Arguments params = new Arguments();
        params.addArgument("EmployeeNo", employeeNo);//设置参数，员工编号
        return params;
    }

    public  String runEmployeeRecycleQuotaTest(String employeeNo) throws Exception {

        RequestMessage rm = new RequestMessage();
        //生成id码 理论上不重复
        Long id = SnowflakeIdGeneratorFactory.getInstance().nextId();
        rm.setReqId("test" + id);
        rm.setFunCode("7014");
        rm.setMerId(MER_ID);
        rm.setVersion(API_VERSION);

        /**--------————————————业务请求参数reqData——————————————————**/
        EmployeeRecycleQuotaModel model = new EmployeeRecycleQuotaModel();
        model.setAmt(1L);
        model.setCause("压测");
        model.setPackageId(1L);//1L、
        model.setEmployeeNo(employeeNo);

        String json = JsonUtils.toJson(model);
        //System.out.println("请求未加密数据:" + json);

        //加密处理
        byte[] bs = DESUtils.encrypt(json.getBytes(CHARSET_UTF_8), INTER_KEY);
        String reqDataEncrypt = Base64Utils.encodeToString(bs);
        rm.setReqData(reqDataEncrypt);
        rm.setSign(RSAUtils.sign(reqDataEncrypt, MER_PRIVATE_KEY));

        String reqStr = JsonUtils.toJson(rm);
        //System.out.println("请求数据：" + reqStr);

        //发送请求
        String resData = HttpClientHelper.sendPost(OMS_TEST_URL, "application/json", reqStr, "POST", CHARSET_UTF_8);
        //System.out.println("响应数据：" + resData);

        return resData;
    }

    @Override
    public SampleResult runTest(JavaSamplerContext javaSamplerContext) {
        SampleResult sampleResult = new SampleResult();
        String resData = "";
        try {
            sampleResult.sampleStart();
            String employeeNo = javaSamplerContext.getParameter("EmployeeNo");
            resData = runEmployeeRecycleQuotaTest(employeeNo);
            //断言
            JSONObject jsonObject1 = JSONObject.parseObject(resData);
            Object code = jsonObject1.get("resCode");
            if (code.equals("0000") || code.equals("6000")) {
                sampleResult.setResponseOK();
                sampleResult.setResponseData(true + employeeNo, SampleResult.TEXT);
                //System.out.println("麦粒回收结果:" + true);
            } else {
                sampleResult.setSuccessful(false);
                sampleResult.setResponseData(false + employeeNo + jsonObject1.get("resMsg"), SampleResult.TEXT);
                //System.out.println("麦粒回收结果:" + false + " " + jsonObject1.get("resMsg"));
            }
        } catch (Exception e) {
            sampleResult.setSuccessful(false);
            sampleResult.setResponseData(resData, SampleResult.TEXT);
            sampleResult.setResponseMessage(e.toString());
        } finally {
            sampleResult.sampleEnd();
        }

        return sampleResult;
    }


    //public static void main(String[] args) {
    //    EmployeeRecycleQuotaTest j = new EmployeeRecycleQuotaTest();
    //    try {
    //        System.out.println(j.runEmployeeRecycleQuotaTest("13300000577"));
    //    } catch (Exception e) {
    //        e.printStackTrace();
    //    }
    //}



}
